<?php

namespace app\home\controller;

use \app\common\controller\Base;
use \think\Validate;

class Index extends Base
{
    //首页
    public function index()
    {
        $staff_name_list = db('staff')
            ->field('id,name')
            ->select();
        $this->assign([
            'staff_name_list' => $staff_name_list
        ]);
        return view();
    }

    public function show_home_data()
    {
        $now_staff_id = input("now_staff_id");
        $now_start_date = input("now_start_date");
        $now_end_date = input("now_end_date");
        $visit_state_on = 0;
        $visit_state_off = 0;
        $intention_id_on = 0;
        $intention_id_off = 0;
        $channel_id_1 = 0;
        $channel_id_2 = 0;
        $channel_id_3 = 0;
        $term = array();
        if (Validate::is($now_start_date, 'date') == 0 || Validate::is($now_end_date, 'date') == 0) {
            $res = json([
                'status' => 0
                , 'msg' => "查询日期错误，请重新选择"
            ]);
            return $res;
        }
        //判断如果非管理员只能看自己
        if (!session("common_show_all_data")) {
            $term['staff_id'] = session("user_id");
        } else {
            if ($now_staff_id != 999) {
                $term['staff_id'] = $now_staff_id;
            }
        }
        $now_start_date = strtotime($now_start_date);
        $now_end_date = strtotime($now_end_date . "+1 day");

        //获取某日期段里面的上门和渠道
        $all_list = db('consultant')
            ->where($term)
            ->where('first_contact_date', 'between', '' . $now_start_date . ',' . $now_end_date . '')
            ->field('visit_state,channel_id')
            ->select();
        //获取某日期段里面的意向情况
        $intention_list = db('consultant')
            ->where($term)
            ->where('visit_state', 1)
            ->where('first_contact_date', 'between', '' . $now_start_date . ',' . $now_end_date . '')
            ->field('intention_id')
            ->select();

        //统计
        foreach ($all_list as $v) {
            if ($v['visit_state'] == 1) {
                $visit_state_on++;
            } else {
                $visit_state_off++;
            }
            if ($v['channel_id'] == 1) {
                $channel_id_1++;
            } else if ($v['channel_id'] == 2) {
                $channel_id_2++;
            } else {
                $channel_id_3++;
            }
        }
        foreach ($intention_list as $v) {
            if ($v['intention_id'] == 1 || $v['intention_id'] == 2) {
                $intention_id_on++;
            } else {
                $intention_id_off++;
            }
        }
        //咨询者个数统计
        $date_arr = array();
        for ($day = 0; $day < 7; $day++) {
            $date_arr[] = date("Y-m-d", strtotime(input("now_end_date") . "-" . $day . " day"));
        }
//        $now_start_date_s = date("Y-m-d", strtotime(input("now_end_date") . "-6 day"));
//        $now_end_date_s = input("now_end_date");
        $staff_list = db('staff')
            ->where('department_id', 4)
            ->field('id,name')
            ->select();
        if ($staff_list) {
            foreach ($staff_list as $v) {
                unset($staff_number);
                $now_staff_num = db('staff_consultant_number')
                    ->where('staff_id', $v['id'])
                    ->where('date', 'between', '' . $date_arr[count($date_arr) - 1] . ',' . $date_arr[0] . '')
                    ->order('id', "asc")
                    ->column('number', 'date');
                for ($now_day = count($date_arr) - 1; $now_day >= 0; $now_day--) {
                    if (array_key_exists($date_arr[$now_day], $now_staff_num)) {
                        $staff_number[] = $now_staff_num[$date_arr[$now_day]];
                    } else {
                        $staff_number[] = 0;
                    }
                }
                $res_all_data[] = ['name' => $v['name'], 'type' => 'line', 'stack' => $v['id'], 'data' => $staff_number];
            }
        }else{
            for ($now_day = count($date_arr) - 1; $now_day >= 0; $now_day--) {
                    $staff_number[] = 0;
            }
            $res_all_data[] = ['name' => 'admin', 'type' => 'line', 'stack' => "-1", 'data' => $staff_number];
        }

        $res_visit_state[] = ['value' => $visit_state_on, 'name' => '上门', 'itemStyle' => $itemStyle[0] = ['normal' => $normal[0] = ['color' => '#4473c5']]];
        $res_visit_state[] = ['value' => $visit_state_off, 'name' => '未上门', 'itemStyle' => $itemStyle[0] = ['normal' => $normal[0] = ['color' => '#9AC400']]];

        $res_intention_id[] = ['value' => $intention_id_on, 'name' => '有意向', 'itemStyle' => $itemStyle[0] = ['normal' => $normal[0] = ['color' => '#E9CD6C']]];
        $res_intention_id[] = ['value' => $intention_id_off, 'name' => '没意向', 'itemStyle' => $itemStyle[0] = ['normal' => $normal[0] = ['color' => '#00C0E4']]];

        $res_channel_id[] = ['value' => $channel_id_1, 'name' => '简历', 'itemStyle' => $itemStyle[0] = ['normal' => $normal[0] = ['color' => '#ED7D31']]];
        $res_channel_id[] = ['value' => $channel_id_2, 'name' => '介绍', 'itemStyle' => $itemStyle[0] = ['normal' => $normal[0] = ['color' => '#A02311']]];
        $res_channel_id[] = ['value' => $channel_id_3, 'name' => '高校资源', 'itemStyle' => $itemStyle[0] = ['normal' => $normal[0] = ['color' => '#444442']]];

        $res_home_data = [
            'res_visit_state' => $res_visit_state
            , 'res_intention_id' => $res_intention_id
            , 'res_channel_id' => $res_channel_id
            , 'res_all_data' => $res_all_data
            , 'date_arr' => array_reverse($date_arr)
        ];
        $res = json([
            'status' => 1
            , 'msg' => "查询成功"
            , 'data' => $res_home_data
        ]);
        return $res;
    }
}